package com.itheima.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.PageResult;
import com.itheima.common.Result;
import com.itheima.entity.Brand;
import com.itheima.entity.SerachDto;
import com.itheima.mapper.BrandMapper;
import com.itheima.service.BrandService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Override
    public List<Brand> findAll() {
        List<Brand> list = this.list();
        return list;
    }

    @Override
    public PageResult findPage(SerachDto serachDto, int page, int size) {
        String name = serachDto.getName();
        Character letter = serachDto.getLetter();


        IPage<Brand> page1 = new Page<>(page, size);
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Brand::getName, name);
        wrapper.eq(letter != null, Brand::getLetter, letter);
        IPage<Brand> page2 = this.page(page1, wrapper);

        List<Brand> records = page2.getRecords();
        long total = page2.getTotal();
        PageResult pageResult = new PageResult(total, records);
        return pageResult;
    }

    @Override
    public Brand findById(Integer id) {
        return this.getById(id);
    }

    @Override
    public Result delete(Integer id) {
        this.removeById(id);
        return new Result(0, true, "删除成功", null);
    }

    @Override
    public Result add(Brand brand) {
        this.save(brand);
        return new Result(0, true, "添加成功", null);
    }

    @Override
    public Result updateBrand(Brand brand) {
        boolean b = this.updateById(brand);
        return new Result(0, true, "修改成功", null);
    }

    @Override
    public List<Brand> fidnList(SerachDto serachDto) {
        String name = serachDto.getName();
        Character letter = serachDto.getLetter();

        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Brand::getName, name);
        wrapper.eq(letter != null, Brand::getLetter, letter);
        List<Brand> list = this.list(wrapper);
        return list;
    }


}
